package main

import (
	"fmt"
	"sync"
)

/**
两个协程交替打印1-100🤹奇偶数，一个奇数，一个偶数
*/

func main() {
	wg := &sync.WaitGroup{}

	channel1 := make(chan int)
	channel2 := make(chan int)
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 0; i <= 100; i++ {
			if i%2 == 0 {
				fmt.Println(fmt.Sprintf("process 1:%d", i))
				if i != 100 {
					channel2 <- 1
					<-channel1
				}
			}
		}
	}()
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 0; i <= 100; i++ {
			if i%2 != 0 {
				<-channel2
				fmt.Println(fmt.Sprintf("process 2:%d", i))
				channel1 <- 1
			}
		}
	}()
	wg.Wait()
}
